Today we are going to go over a bunch of stuff I thought was interesting but didn’t fit specifically into any of the other lessons. This includes some cool ggplot extension packages we haven’t gone over yet, and heatmaps that utilize base R plotting.
Artwork by @allison_horst
Loading the libraries that are for each section. Individual libraries are before each section so you can see which go with what plot types.
library(tidyverse) # for everything
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.1 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Artwork by @allison_horst
I have noticed that many of you are still not using RProjects. I would really recommend that for easy file management that you do. Here is an a chapter in R for Data Science on how to set one up. If you want to start using Git in the future, you will need to set up a project.
Artwork by @allison_horst
The package gghighlight
allows you to highlight certain geoms in ggplot. Doing this helps your
reader focus on the thing you want them to, and helps prevent plot
spaghetti. To practice with gghighlight we are going to use
some data from the R package gapminder
installl.packages("gghighlight")
install.packages("gapminder")
First let’s load our libraries.
library(gghighlight) # for highlighting
library(gapminder) # where data is
We can create a dataframe that includes only the data for the countries in the continent Americas.
gapminder_americas <- gapminder %>%
filter(continent == "Americas")
If we look at all the countries at once, we get plot spaghetti 🍝.
gapminder_americas %>%
ggplot(aes(x = year, y = lifeExp, group = country, color = country)) +
geom_line() +
theme_minimal() +
labs(x = "Year",
y = "Life Expectancy (years)",
title = "Life Expectancy in Countries in the Americas",
subtitle = "From 1952 to 2007",
caption = "Data from gapminder.org")
Create a lineplot showing the life expectacy over 1952 to 2007 for all countries, highlighting the United States.
# highlight just the US
gapminder_americas %>%
ggplot(aes(x = year, y = lifeExp, group = country, color = country)) +
geom_line() +
gghighlight(country == "United States") +
theme_minimal() +
labs(x = "Year",
y = "Life Expectancy (years)",
title = "Life Expectancy in Countries in the Americas",
subtitle = "From 1952 to 2007",
caption = "Data from gapminder.org")
Facet our plot, and highlight the country for each facet.
# facet and highlight each country
gapminder_americas %>%
ggplot(aes(x = year, y = lifeExp)) +
geom_line(aes(color = country)) +
gghighlight() +
theme_minimal() +
theme(legend.position = "none",
strip.text.x = element_text(size = 8),
axis.text.x = element_text(angle = 90)) +
facet_wrap(~country) +
labs(x = "Year",
y = "Life Expectancy (years)",
title = "Life Expectancy in Countries in the Americas",
subtitle = "From 1952 to 2007",
caption = "Data from gapminder.org")